﻿using System;
using System.Collections.Generic;
using System.Linq;
using Web.Models;
using Model.Core.DataBase;
using Web.Models.AnalysisData;
using Web.Models.CoreData;


namespace Web.BML.CoreData
{
    /// <summary>
    /// 系统用户
    /// </summary>
    public class SysUserOp
    {

        private static SysUserOp instance;
        /// <summary>
        /// 构造函数
        /// </summary>
        private SysUserOp()
        {

        }
        /// <summary>
        /// 单例模式实例化
        /// </summary>
        /// <returns></returns>
        public static SysUserOp GetInstance()
        {
            if (instance == null)
            {
                instance = new SysUserOp();
            }
            return instance;
        }

        

        /// <summary>
        ///    查询用户
        /// </summary>
        /// <param name="id">用户id</param>
        /// <returns></returns>
        public SysUser QueryUser(Guid id)
        {
            using (IDbContext db = ContainerFactory.OpenCoreConnect())
            {
                return db.Queryable<SysUser>().FirstOrDefault(m => m.Id == id);
            }
        }

        /// <summary>
        ///    登录，查询用户
        /// </summary>
        /// <param name="account">用户许可号</param>
        /// /// <param name="password">用户密码</param>
        /// <returns></returns>
        public SysUser QueryUser(String account,String password)
        {
            using (IDbContext db = ContainerFactory.OpenCoreConnect())
            {
                return db.Queryable<SysUser>().FirstOrDefault(m => m.Account == account&&m.Password == password);
            }
        }

        /// <summary>
        /// 查询所有用户
        /// </summary>
        /// <returns></returns>
        public List<SysUser> QueryAllUser()
        {
            using (IDbContext db = ContainerFactory.OpenCoreConnect())
            {
                return db.Queryable<SysUser>().ToList();
            }
        }
        
        /// <summary>
        /// 修改用户
        /// </summary>
        /// <param name="model"></param>
        public void ModifUser(SysUser model)
        {
            using (IDbContext db = ContainerFactory.OpenCoreConnect())
            {
                db.GetOperator<SysUser>().Modify(model);
                db.SaveAllChange();
            }
        }

        /// <summary>
        /// 创建用户
        /// </summary>
        /// <param name="model"></param>
        public void CreateUser(SysUser model)
        {
            using (IDbContext db = ContainerFactory.OpenCoreConnect())
            {
                db.GetOperator<SysUser>().Create(model);
                db.SaveAllChange();
            }
        }
    }


}

